/**
 * @(#) PacdpProxy.as
 *
 * @date		2012. 09. 19.
 * @version		1.0
 * @author		Park, Jutae
 * @description	 CDP신청 Proxy - Proxy
 * 				CDP 신청 리스트를 조회하고 저장, 수정, 삭제한다.
 */
package com.skt.common_hr.skbcomm.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.skbcomm.SkbcommFacade;
	import com.skt.common_hr.skbcomm.model.vo.SkbBillingVO;
	import com.skt.common_hr.skbcomm.model.vo.SkbSaveVO;
	import com.skt.common_hr.skbcomm.model.vo.SkbVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class SkbCommProxy extends CommonProxy
	{
		public static const NAME:String = "SkbCommProxy";
		public static const LOAD_STATEMENT_URL_COMPLTE:String		= "loadStatementUrlComplete";
		public static const LOAD_STATEMENT_URL_FAILED:String		= "loadStatementUrlFailed";
		
		public var inrecList:ArrayCollection;
		public var sinChungList:ArrayCollection;
		public var acMedYear:ArrayCollection;
		public var sRettext:String;
		public var readList:ArrayCollection;
		public var billingList:ArrayCollection;
		public var errCode:int;
		public var errText:String;
		public var url:String;
		
		public function SkbCommProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function loadCheck(_pernr:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = _pernr;
			loadData("Z_HR_NT04", params, loadCheckResult, loadFault); 
		}
		
		public function loadBillingCall(_pernr:String, currentYear:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = _pernr;
			params["I_GJAHR"] = currentYear;
			loadData("Z_HR_NT03", params, loadBillingResult, loadFault); 
		}
		
		public function loadBillingComboCall(_pernr:String, currentYear:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = _pernr;
			params["I_GJAHR"] = currentYear;
			loadData("Z_HR_NT03", params, loadBillingComboResult, loadFault); 
		}
		
		public function load(pernr:String, sDay:Date, eDay:Date):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_BEGDA"] = DateUtil.toBasicString(sDay);
			params["I_ENDDA"] = DateUtil.toBasicString(eDay);
			
			loadData("Z_HR_NT01", params, loadResult, loadFault); 
		}
		
		public function insert(rec:SkbVO):void
		{
			
			var params:Object = new Object();
			var obj:Object = new Object();
			params["I_ACTIO"] = "SAVE"; 
			
			var tables:Object = new Object();
			var table:Array = new Array();
			
			obj["PERNR"]				= rec.PERNR;
			obj["DATUM"]				= DateUtil.toBasicString(rec.DATUM);
			obj["RQMON"]				= rec.RQMON;
			obj["ACCNO"]				= rec.ACCNO;
			obj["NOMIN"]				= rec.NOMIN;
			obj["RELAT"]				= rec.RELAT;
			
			table.push(obj);
			
			tables["T_IN"] = table;
			
			saveData("Z_HR_NT02", params, tables, insertResult, insertFault);
		}
		
		public function readLoad(rec:SkbVO):void
		{
			var params:Object = new Object();
			var obj:Object = new Object();
			params["I_ACTIO"] = "DIS"; 
			
			var tables:Object = new Object();
			var table:Array = new Array();
			
			obj["PERNR"]				= rec.PERNR;
			obj["DATUM"]				= DateUtil.toBasicString(rec.DATUM);
			obj["RQMON"]				= rec.RQMON;
			obj["ACCNO"]				= rec.ACCNO;
			obj["NOMIN"]				= rec.NOMIN;
			obj["RELAT"]				= rec.RELAT;
			
			table.push(obj);
			
			tables["T_IN"] = table;

			saveData("Z_HR_NT02", params, tables, loadResult1, insertFault); 
			
		}

		public function del(rec:SkbVO):void
		{ 
			var params:Object = new Object();
			params["I_ACTIO"] = "DEL"; 			
			var obj:Object = new Object();
			var tables:Object = new Object();
			var table:Array = new Array();
			obj["PERNR"]				= rec.PERNR;
			obj["DATUM"]				= DateUtil.toBasicString(rec.DATUM);
			obj["RQMON"]				= rec.RQMON;
			obj["ACCNO"]				= rec.ACCNO;
			obj["NOMIN"]				= rec.NOMIN;
			obj["RELAT"]				= rec.RELAT;
			obj["STATS"]				= rec.STATS;
			table.push(obj);
			
			tables["T_IN"] = table;

			saveData("Z_HR_NT02", params, tables, delResult, delFault); 

		}
		
		public function end(rec:SkbVO):void
		{ 
			var params:Object = new Object();
			params["I_ACTIO"] = "END"; 			
			var obj:Object = new Object();
			var tables:Object = new Object();
			var table:Array = new Array();
			obj["PERNR"]				= rec.PERNR;
			obj["DATUM"]				= DateUtil.toBasicString(rec.DATUM);
			obj["RQMON"]				= rec.RQMON;
			obj["ACCNO"]				= rec.ACCNO;
			obj["NOMIN"]				= rec.NOMIN;
			obj["RELAT"]				= rec.RELAT;
			obj["STATS"]				= rec.STATS;
			table.push(obj);
			
			tables["T_IN"] = table;

			saveData("Z_HR_NT02", params, tables, delResult, delFault); 

		}
		
		public function loadAttach(pernr:String, _obj:SkbVO):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = Variables.myPernr;
//			param["I_BEGDA"] = DateUtil.toBasicString(_obj.BEGDA);
//			param["I_ENDDA"] = DateUtil.toBasicString(_obj.ENDDA);
//			param["I_SEQNR"] = _obj.SEQNR;
			
			loadData("Z_HR_IN04", param, resultAttach, fault); 
		}
		
		public function loadCheckResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(SkbcommFacade.LOAD_CHECK_COMPLETE, this);
			}
			else {
				failedCheckAlert = event.result.RETTEXT;
				facade.sendNotification(SkbcommFacade.LOAD_CHECK_FAILED, this);
			}
		}
		
		public function loadBillingResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				billingList = parseBillingList(event.result.T_OUT);
				acMedYear = event.result.T_CODE;
				facade.sendNotification(SkbcommFacade.LOAD_READ_COMPLETE, this);
			}
			else {
				failedCheckAlert = event.result.RETTEXT;
				facade.sendNotification(SkbcommFacade.LOAD_READ_FAILED, this);
			}
		}
		
		public function loadBillingComboResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				billingList = parseBillingList(event.result.T_OUT);
				facade.sendNotification(SkbcommFacade.LOAD_READ_COMBO_COMPLETE, this);
			}
			else {
				failedCheckAlert = event.result.RETTEXT;
				facade.sendNotification(SkbcommFacade.LOAD_READ_FAILED, this);
			}
		}
		

		public function loadResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				inrecList = parseInrecList(event.result.T_OUT);
				facade.sendNotification(SkbcommFacade.LOAD_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(SkbcommFacade.LOAD_LIST_FAILED, this);
			}
		}
		
		public function loadResult1(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  	
				readList 		= event.result.T_CODE;
				facade.sendNotification(SkbcommFacade.READ_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(SkbcommFacade.READ_LIST_FAILED, this);
			}
		}
		
		public function resultAttach(event:ResultEvent):void
		{
			errCode = event.result["RETCODE"];
			errText = event.result["RETTEXT"];
			if(event.result["RETCODE"]==0) {
				url = StringUtil.NVL(event.result["O_URL"]);
				facade.sendNotification(SkbCommProxy.LOAD_STATEMENT_URL_COMPLTE, this);
			}
			else {
				trace( "failed to 정산전표 URL");
				url = "";
				facade.sendNotification(SkbCommProxy.LOAD_STATEMENT_URL_FAILED, this);
			}
		}

		private function fault(event:FaultEvent):void
		{
			trace(event.fault.faultString);
			errCode = -1;
			errText = "호출실패";
			url = "";
			facade.sendNotification(SkbCommProxy.LOAD_STATEMENT_URL_FAILED, this);
		}
		
		private function loadFault(event:FaultEvent):void
		{
			facade.sendNotification(SkbcommFacade.LOAD_LIST_FAILED, this);
		}
		
		public var failedAlert:String;
		public var failedCheckAlert:String; 
		public function insertResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(SkbcommFacade.INSERT_COMPLETE, this);
			}
			else {
				failedAlert = event.result.RETTEXT;
				facade.sendNotification(SkbcommFacade.CANCEL_REQUEST_INPUT, this);
			}
		}
		
		private function insertFault(event:FaultEvent):void
		{
			facade.sendNotification(SkbcommFacade.INSERT_FAILED, this);
		}
		public var sDelFaultMsg:String='';
		public function delResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(SkbcommFacade.DELETE_COMPLETE, this);
			}
			else {
				sDelFaultMsg = event.result.RETTEXT;
				facade.sendNotification(SkbcommFacade.DELETE_FAILED, this);
			}
		}
		
		private function delFault(event:FaultEvent):void
		{
			facade.sendNotification(SkbcommFacade.DELETE_FAILED, this);
		}
		
		private function parseInrecList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:SkbVO = new SkbVO();
				vo.ACCNO		= rec["ACCNO"];
				vo.PERNR		= rec["PERNR"];
				vo.DATUM 		= DateUtil.stringToDateTime(rec["DATUM"]);
				vo.LTAMT		= rec["LTAMT"];
				vo.NOMIN 		= rec["NOMIN"];
				vo.REASN		= rec["REASN"];
				vo.RELAT		= rec["RELAT"];
				vo.RELAX	 	= rec["RELAX"];
				vo.RQMON	 	= rec["RQMON"];
				vo.STATS		= rec["STATS"];
				vo.STATX		= rec["STATX"];
				
				// 변경삭제 유무 
				if(vo.STATS == '1'){
					vo.STATS_F = true;
					vo.STATS_D = false;
				} else if(vo.STATS == '3'){
					vo.STATS_D = true;
					vo.STATS_F = false;
				} else {
					vo.STATS_F = false;
					vo.STATS_D = false;
				}	
				
				retArr.addItem(vo);
			}
			return retArr;
		}
		
		private function parseBillingList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:SkbBillingVO = new SkbBillingVO();
				vo.RQMON		= rec["RQMON"];
				vo.APAMT		= rec["APAMT"];
				vo.SPAMT		= rec["SPAMT"];
				
				retArr.addItem(vo);
			}
			return retArr;
		}
		
		private function parseCause(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();


			for each(var obj:Object in recArr){
				var retObj:SkbSaveVO = new SkbSaveVO();
				for(var field:String in obj) {
				     if(retObj.hasOwnProperty(field)==false ) {
				      continue;
				     }
				     
					if(field == "BEGDA") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					else if(field == "ENDDA") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					
					else {
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		}
		
		private function parseSinchungList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:SkbVO = new SkbVO();
//				vo.ORGTX 		= rec["ORGTX"];
//				vo.PERNR		= rec["PERNR"];
//				vo.ENAME		= rec["ENAME"];
//				vo.PTEXT 		= rec["PTEXT"];
//				vo.INTEL		= rec["INTEL"];
				
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
		private function parseYear(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr)
			{
				retArr.addItem({label:rec["TEXT"], data:rec["CODE"]});
			}
			return retArr;
		}
		
		private function parseList(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();


			for each(var obj:Object in recArr){
				var retObj:SkbVO = new SkbVO();
				for(var field:String in obj) {
				     if(retObj.hasOwnProperty(field)==false ) {
				      continue;
				     }
				     
					if(field == "BEGDA") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					else if(field == "ENDDA") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					else {
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		}
		
		private function parseList2(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();


			for each(var obj:Object in recArr){
				var retObj:SkbSaveVO = new SkbSaveVO();
				for(var field:String in obj) {
				     if(retObj.hasOwnProperty(field)==false ) {
				      continue;
				     }
				     
					if(field == "BEGDA") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					else if(field == "ENDDA") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					else {
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		}
				
	}
}